<?php
	include_once '../../config.php';

/**
 * return array(array('actionName' => 'string',
  	 					'productID'=> 'string',
  	 					'origion_price' => 'double',
  	 					'status'=> 'string(open,close)',
  	 					'timeleft' => 'long',
  	 					'aution_price'=> 'double',
  	 					'current_user_bid'=> 'userID: string',
  	 					'price_per_bid' => 'int'))
 */
$error = false;
$connection = mysql_connect ($host, $username, $password) or die ( $connect_error . mysql_error () );
$auctionList = ! empty ( $_POST ['data'] ) ? $_POST ['data'] : "";

$auctionList = explode ( ",", $auctionList );
foreach ( $auctionList as &$auction ) {
	$auction = ( int ) $auction;
}

$auctionList = implode ( ",", $auctionList );

mysql_select_db ( $db, $connection ) or die ( $connect_error . mysql_error () );
$delete_result = @mysql_query ( "delete from Auction WHERE auctionID IN (" . $auctionList . " )", $connection );

if (!$delete_result) $error =true;


if (!$delete_result) $error =false;

$result = @mysql_query ( "select * from auction_view WHERE auctionID", $connection );
if (!$result) $error = true;
$arr = array ();
while ( $row = @mysql_fetch_assoc ( $result ) ) {
	
	$status = "open";
	if ( strtotime($row ['end_time'])< strtotime('now') )
		 $status = "closed";
	if (strtotime($row ['start_time'])> strtotime('now'))
		$status = "Not started";
	
	
	$arr = array_merge ( $arr, array (
			array (
					'auctionID' => $row['auctionID'],
					'name' => $row['name'],
					'pro_name'=>$row['pro_name'],
					'status' =>	$status,
					'start_time' => $row ['start_time'] ,	
					'end_time' => $row ['end_time'] ,
					'price' => ltrim($row ['price'],'0'),
					'auction_price' => ltrim($row ['auction_price'],'0'),
					'quantity' => $row['quantity'],
					'current_user_bid' => $row ['current_user_bid'],
					'price_per_bid' => $row ['price_per_bid'],
					'error' =>$error
			) 
	) );
}
mysql_close($connection);
die ( json_encode ( $arr ) );
	
